package com.yiip.kafka.test.producter;

import org.apache.kafka.clients.producer.*;

import java.util.Properties;

public class MyProducter {
    public static void main(String[] args) {

        Properties properties = new Properties();
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "master:9092");
        properties.put("acks", "all");
        properties.put("retries", 1);//重试次数
        properties.put("batch.size", 16384);//批次大小
        properties.put("linger.ms", 1);//等待时间
        properties.put("buffer.memory", 33554432);//RecordAccumulator缓冲区大小

        properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<>(properties);

        for (int i = 0; i < 10; i++) {
            kafkaProducer.send(new ProducerRecord<String, String>("test", "hello--" + i),
                    (recordMetadata, e) -> {
                        if (e == null) {
                            System.out.println("进入到结束方法");
                        }
                    }
            );
        }

        kafkaProducer.close();
    }
}
